﻿using System;
using System.Collections.Generic;

namespace Mitosoft.Definitions.ExtendedBaseValues
{
	/// <summary>
	/// Represents a generic property already implementing an event
	/// being thrown when Value changes.
	/// </summary>
	/// <typeparam name="TO">Represents the property type.</typeparam>
	public class ExtendedProperty<TO> : IExtendedProperty<TO>
	{
		#region events

		/// <summary>
		/// Will be thrown when <see cref="Value"/> changes.
		/// </summary>
		public event EventHandler<TO> ValueChanged;

		#endregion

		#region fields

		/// <summary>
		/// Represents <see cref="Value"/>
		/// </summary>
		protected TO _value;

		#endregion

		#region properties

		/// <summary>
		/// Gets or sets the value
		/// </summary>
		public TO Value
		{
			get
			{
				return _value;
			}
			set
			{
				if( EqualityComparer<TO>.Default.Equals( _value, value ) )
					return;	// Nothing changed.

				_value = value;
				
				var handler = ValueChanged;
				if (handler == null) return;
				handler(this, _value);
			}
		}

		#endregion
	}
}
